package com.exam.role.action;

import java.util.List;

import com.exam.base.action.BaseAction;
import com.exam.role.model.Module;
import com.exam.role.model.Role;
import com.exam.role.model.Student;
import com.exam.role.model.Teacher;
import com.exam.role.service.RoleService;

public class RoleAction extends BaseAction {
	private RoleService roleService;
	private List<Role> roles;
	private Role role = new Role();
	private List<Module> modules;
	private List<Student> students;
	private List<Teacher> teachers;
	private Student student = new Student();
	private Teacher teacher = new Teacher();

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public Role getRole() {
		return role;
	}

	public List<Module> getModules() {
		return modules;
	}

	public void setModules(List<Module> modules) {
		this.modules = modules;
	}

	public List<Student> getStudents() {
		return students;
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}

	public List<Teacher> getTeachers() {
		return teachers;
	}

	public void setTeachers(List<Teacher> teachers) {
		this.teachers = teachers;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * 查询
	 * 
	 * @return
	 */
	public String search() {
		setRoles(roleService.findRolesByPage(role, getPage()));
		return SUCCESS;
	}

	/**
	 * 维护权限菜单
	 * 
	 * @return
	 */
	public String editMenus() {
		int roleId = Integer.parseInt(getRequest().getParameter("rid"));
		setModules(roleService.getMenus(roleId));
		setRole(roleService.getRole(roleId));
		return "editMenus";
	}

	/**
	 * 保存对 权限对应的菜单组 的修改结果
	 * 
	 * @return
	 */
	public String saveMenus() {
		String[] parents = getRequest().getParameterValues("#module.haveRole");
		String[] sons = getRequest().getParameterValues("#sonModule.haveRole");
		roleService.saveMenus(parents, sons, role.getId());
		setModules(roleService.getMenus(role.getId()));
		return "editMenus";
	}

	/**
	 * 维护权限人员
	 * 
	 * @return
	 */
	public String editTeachers() {
		int roleId = Integer.parseInt(getRequest().getParameter("rid"));
		setRole(roleService.getRole(roleId));
		setTeachers(roleService.getTeacherByRole(roleId, teacher, getPage()));
		return "editTeachers";
	}

	public String editStudents() {
		int roleId = Integer.parseInt(getRequest().getParameter("rid"));
		setRole(roleService.getRole(roleId));
		setStudents(roleService.getStudentByRole(roleId, student, getPage()));
		return "editStudents";
	}

	public String addTeacherToRole() {
		setRole(roleService.getRole(role.getId()));
		// setStudents();
		return "editStudents";
	}

	public String addStudentToRole() {
		int roleId = Integer.parseInt(getRequest().getParameter("rid"));
		setStudents(roleService.getStudentByRole(roleId, student, getPage()));
		return "editStudents";
	}

	/**
	 * 删除权限人员
	 * 
	 * @return
	 */
	public String removeTeacherFromRole() {
		int roleId = Integer.parseInt(getRequest().getParameter("rid"));
		setStudents(roleService.getStudentByRole(roleId, student, getPage()));
		return "editStudents";
	}

	public String removeStudentFromRole() {
		int roleId = Integer.parseInt(getRequest().getParameter("rid"));
		setStudents(roleService.getStudentByRole(roleId, student, getPage()));
		return "editStudents";
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
}
